from pymysql import Connection
import configparser

config = configparser.ConfigParser()
config.read("mysql_info.ini", encoding="utf-8")

# 转驼峰
def to_lower_camel(str: str):
    str = str.replace("_", " ").title().replace(" ", "")
    return str[0].lower() + str[1:]

# 转驼峰
def to_lower_camel_list(strList):
    return [to_lower_camel(s) for s in strList]

# 输出流
def file_str_io(adders: str,data: str):
    try:
        op = open(adders, "w", encoding="UTF-8")
        op.write(data);
    except Exception as e:
        print(e)
    finally:
        if op:
            op.close()


# 配置数据库相关信息
def mysql_connection():
    try:
        conn = Connection(
            host=config["section-mysql-info"]["host"],
            port=int(config["section-mysql-info"]["port"]),
            user=config["section-mysql-info"]["user"],
            password=config["section-mysql-info"]["password"]
        )
        # 选择数据库
        conn.select_db(config["section-mysql-info"]["db"])
        return conn
    except Exception as e:
        print(e)
        return None
